#include "Worker.h"
#include "../../Core/Application/Program.h"

using namespace skelepp::Core::Application;
using namespace skelepp::Core::Tasking;

Worker::Worker(void) {
	_current = NULL;
	_busy = false;
	pthread_mutex_init(&_lock, NULL);
	pthread_create(&_worker, NULL, _run, this);
}

Worker::~Worker(void) {

}

void Worker::start(void) {

}

void Worker::stop(void) {

}

void Worker::run(void) {
	while (Program::get()->is_running()) {
		if (_current) {
			_busy = true;
			_current->perform();
			_current = NULL;
			_busy = false;
		}
		yield();
	}
	pthread_exit(NULL);
}

void Worker::perform(_Task *task) {
	_current = task;
}

bool Worker::is_busy(void) {
	return _busy;
}